#include "Tank.h"

int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPreaInstance, LPSTR lpCmdLine, int nCmdShow)
{
    g_hInstance = hInstance;
    //做一个窗口
    //设计窗口类
    TCHAR szAppClassName[] = TEXT("DK");

    WNDCLASS wc = {0};
    wc.hbrBackground = CreateSolidBrush(RGB(0,0,0));            //黑色背景
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);  //加载光标
    wc.hInstance = hInstance;                                       //当前应用程序实例句柄
    wc.lpfnWndProc = WindowProc;                                   //窗口处理函数
    wc.lpszClassName = szAppClassName;                            //窗口类型名
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;             //窗口类的风格

    //注册窗口类
    RegisterClass(&wc);
    //创建窗口
    TCHAR szWindowTitle[] = TEXT("tank");
    HWND hwnd = CreateWindow(szAppClassName, szWindowTitle, WS_BORDER | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, 300, 200, 700, 600, NULL, NULL, hInstance, NULL);   //窗口类型名和标题
    //显示窗口和更新窗口
    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);
    //消息循环
    MSG msg;
    while (GetMessage(&msg,NULL,0,0))
    {
        //将虚拟键消息转化为字符消息
        TranslateMessage(&msg);
        //将消息分发给窗口处理函数
        DispatchMessage(&msg);
    }


    return 0;
}



//窗口处理函数
LRESULT  CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    g_hWnd = hwnd;
    PAINTSTRUCT ps;
    HDC hdc;
    switch (uMsg)
    {
        case WM_CREATE://窗口创建消息
            InitTank(&g_tank);
            break;
        case WM_PAINT://窗口绘图消息
        //开启绘图
          hdc  =  BeginPaint(hwnd, &ps);
            DrawGame(hdc);
            //结束绘图
            EndPaint(hwnd, &ps);
            break;
        case WM_KEYDOWN://移动坦克
            switch (wParam)
            {
                case VK_LEFT://左键
                g_tank.direction  = LEFT; g_tank.x -= 10;
                    break;
                    case VK_RIGHT://右键
                g_tank.direction  = RIGHT; g_tank.x += 10;
                    break;
                    case VK_UP://上键
                g_tank.direction  = UP; g_tank.y -= 10;
                    break;
                    case VK_DOWN://下键
                g_tank.direction  = DOWN; g_tank.y += 10;
                    break;
            }
            InvalidateRect(hwnd,NULL,TRUE);
//        case WM_CREATE://窗口创建消息
//        {//GDI+资源初始化
//            GdiplusStartupInput gdiplusStartupInput;
//            GdiplusStartup(&gdiplusToken, &gdiplusStartupInput,NULL);
//            break;
//        }
        case WM_CLOSE://窗口关闭消息
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY://窗口销毁消息
        {
//            销毁GDI+资源
//            GdiplusShutdown(gdiplusToken);
            PostQuitMessage(0);
            break;
        }
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void DrawGame(HDC hdc)
{
    DrawTank(hdc, &g_tank);
}


//初始化坦克
void InitTank(Tank* tank)
{
    //获取窗口客户区大小
    RECT rect;
    GetClientRect(g_hWnd,&rect);

    //坦克的中心点绘制
    tank ->x = (rect.right - rect.left);
    tank ->y = (rect.bottom - rect.top) - TANK_HEIGHT / 2;
    tank ->direction = UP;
    tank ->life = 9;
}


//绘制坦克
void DrawTank(HDC hdc,Tank* tank)
{

    //首先加载图片
    HBITMAP hMytank;
    hMytank = (HBITMAP) LoadImage(g_hInstance, reinterpret_cast<LPCSTR>(L"p1tankD"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    //创建内存DC

    HDC hMemDC = CreateCompatibleDC(hdc);
    SelectObject(hMemDC, hMytank);
    //透明贴图法
    switch (tank ->direction)
    {
        case UP:
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 0, 0,194,194,SRCAND);
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 0, 194,194,194,SRCPAINT);
            break;
        case RIGHT:
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 194, 0,194,194,SRCAND);
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 194, 194,194,194,SRCPAINT);
            break;
        case DOWN:
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 388, 0,194,194,SRCAND);
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 388, 194,194,194,SRCPAINT);
            break;
        case LEFT:
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 582, 0,194,194,SRCAND);
            StretchBlt(hdc, tank->x - TANK_WIDTH / 2, tank->y - TANK_HEIGHT  / 2, TANK_WIDTH,TANK_HEIGHT,  hMemDC, 582, 194,194,194,SRCPAINT);
            break;
    }


}